<?php
  namespace app\admin\controller;

    use app\admin\model\ApiCompanyModel;
    use app\admin\model\ApiProjectModel;
    use app\admin\model\ApiResumeModel;
    use app\admin\model\ApiSpotModel;
    use app\admin\model\ApiSupplierModel;
    use app\admin\model\ApiWordModel;
    use app\admin\validate\ApiResumeValidate;
    use app\admin\validate\CommonValidate;
    use think\Db;

    /**
     * Func 操作类
     *
     * Class ApiResumeController
     *
     * @package app\admin\controller
     */
    class ApiResumeController extends BaseController
    {
        /**
         * Func 列表
         */
        public function index()
        {
            $param = $this->request->param();

            // 时间
            if ($this->start_time && $this->end_time)
            {
                $condition['a.create_time'] = ['between', [
                    strtotime($this->start_time), strtotime($this->end_time),
                ]];
            }

            // 关键词
            if ($this->keyword)
            {
                if ($this->searchType)
                {
                    $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
                } else {
                    $condition['a.resume_name'] = ['like', '%' . $this->keyword . '%'];
                }
            }

            // 游玩日期
            if ($this->play_date)
            {
                $condition['a.resume_date'] = ['eq', $this->play_date];
            }

            // 状态
            if ($this->status)
            {
                $condition['a.status'] = ['eq', $this->status];
            }

            // 可查看的分销商id
            $supplierIdArr = (new ApiSupplierModel())->getSupplierIdArr();

            $condition['a.id'] = ['egt', 0];
            $condition['a.supplier_id'] = ['in', $supplierIdArr];

            // 字段
            $fieldArr = ['a.*', 'b.user_login','c.supplier_title','d.spot_title',];

            // 连接表
            $joinArr = [
                ['user b', 'a.user_id = b.id', 'left'],
                ['api_supplier c', 'a.supplier_id = c.id', 'left'],
                ['api_spot d', 'a.spot_id = d.id', 'left'],
            ];

            $reobj = new ApiResumeModel();

            $infos['list'] = $reobj->getApiResumeListInfo(
                $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
                $fieldArr, $joinArr
            );

            $infos['waitCount'] = $reobj->getApiResumeCount(array_merge($condition, ['a.status' => ['eq', 3]]),$joinArr); // 待审核

            $infos['idTypeArr'] = self::$idTypeArr;
            $infos['resumStatusArr'] = self::$resumeStatusArr;

            $this->assign('infos', $infos);

            return $this->fetch('index');
        }

        /**
         * Func 添加
         */
        public function add()
        {
            $spobj = new ApiSpotModel();
            $suobj = new ApiSupplierModel();
            $probj = new ApiProjectModel();
            $srobj = new ApiResumeModel();

            if ($this->request->isPost())
            {
                $param = $this->request->param();

                $result = $this->validate($param, 'ApiResume.add');
                if (!is_bool($result))
                {
                    $this->error($result);
                }

                if (!in_array($param['id_type'], array_keys(self::$idTypeArr)))
                {
                    $this->error('证件类型值错误');
                }

                $vailDate = new CommonValidate();

                // 身份证格式验证
                if($param['id_type'] == 1 && !$vailDate->checkIdCard($param['resume_number']))
                {
                    $this->error('身份证格式错误');
                }
                // 身份证格式验证
                if($param['id_type'] == 2 && !$vailDate->checkPassport($param['resume_number']))
                {
                    $this->error('护照格式错误');
                }

                // 过滤证件号码
                $conditionId['a.id_type'] = ['eq', $param['id_type']];
                $conditionId['a.status'] = ['eq', 1];
                $getIdNumberArr = (new ApiWordModel())->getIdNumberArr($conditionId);
                if($getIdNumberArr)
                {
                    $useIdNumberArr = [];
                    for ($i = 1; $i <= strlen($param['resume_number']); $i++)
                    {
                        $useIdNumberArr[] = substr($param['resume_number'], 0, $i);
                    }
                    $intersection = array_intersect($getIdNumberArr,$useIdNumberArr);
                    if($intersection)
                    {
                        $this->error('该证件号不能录入，请联系平台运营');
                    }
                }

                // 城市验证
                if(isset($param['resume_mobile']) && $param['resume_mobile'])
                {
                    $getIdNumberArr = (new ApiWordModel())->getIdNumberArr(['a.id_type' => 3, 'a.status' => 1]);

                    if($getIdNumberArr)
                    {
                        $getMobileQuery = $this->getMobileQuery(['mobile'=>$param['resume_mobile']]);
                        if (!$getMobileQuery['status'])
                        {
                            $this->error($getMobileQuery['message']);
                        }
                        if(in_array($getMobileQuery['data']['city'],$getIdNumberArr))
                        {
                            $this->error('手机号不能录入，请联系平台运营');
                        }
                    }
                }

                // 景区
                $conditionTO['a.uuid']     = ['eq', $param['spot_uuid']];
                $conditionTO['a.status'] = ['eq', 1];
                $getApispotOneInfo = $spobj->getApispotOneInfo(
                    $conditionTO, ['a.id' => 'desc'], ['a.id', 'a.spot_title']
                );
                if (!$getApispotOneInfo)
                {
                    $this->error('景区不存在或已关闭');
                }

                // 景区项目
                $lastProjectOneInfo = $probj->lastProjectOneInfo(
                    ['spot_id'=>$getApispotOneInfo['id'],'date'=>$param['resume_date']]
                );
                if(!$lastProjectOneInfo)
                {
                    $this->error('该日期未发布出团');
                }

                // 分销商
                $conditionSO['a.uuid']   = ['eq', $param['supplier_uuid']];
                $conditionSO['a.status'] = ['eq', 1];
                $getApiSupplierOneInfo = $suobj->getApiSupplierOneInfo(
                    $conditionSO, ['a.id' => 'desc'], ['a.id','a.user_id', 'a.supplier_title']
                );
                if (!$getApiSupplierOneInfo)
                {
                    $this->error('分销商不存在或已关闭');
                }

                // 查询数据是否存在
                $conditionRC['a.spot_id']   = array('eq', $getApispotOneInfo['id']);
                $conditionRC['a.resume_date']   = array('eq', trim($param['resume_date']));
                $conditionRC['a.resume_number'] = array('eq', trim($param['resume_number']));
                $getApiResumeOneInfo1 = $srobj->getApiResumeOneInfo($conditionRC,['a.id'=>'desc'],['id']);
                if ($getApiResumeOneInfo1)
                {
                    $this->error('【'.$param['resume_date'].'】证件号已存在');
                }

                // 添加和更新
                $data1 = self::getPostParamInfo([
                    'resume_name', 'resume_sex', 'resume_age','id_type', 'resume_number',
                    'resume_mobile', 'resume_date', 'resume_desc'
                ]);

                // 附加参数
                $data2['user_id']       = $getApiSupplierOneInfo['user_id'];
                $data2['spot_id']       = $getApispotOneInfo['id'];
                $data2['supplier_id']   = $getApiSupplierOneInfo['id'];
                $data2['project_id']    = $lastProjectOneInfo['id'];
                $data2['handle_time']   = time();
                $data2['status']        = 1;

                // 添加的数据
                $data = array_merge((array)$data1, (array)$data2);

                $reobj = new ApiResumeModel();

                // 查询数据是否存在
                $conditionRC['a.supplier_id']   = array('eq', trim($data['supplier_id']));
                $conditionRC['a.resume_date']   = array('eq', trim($data['resume_date']));
                $conditionRC['a.resume_number'] = array('eq', trim($data['resume_number']));
                $getApiResumeCount = $reobj->getApiResumeCount($conditionRC);
                if ($getApiResumeCount)
                {
                    $this->error('【'.$data['resume_date'].'】证件号已存在');
                }
                if ($reobj->addOrUpdateApiResumeInfo($data))
                {
                    $probj->triggerProjectUpdateData($lastProjectOneInfo['id']);

                    $this->success('添加成功!', url('index', []));
                } else {
                    $this->error('添加失败,请稍后重试');
                }
            }

            // 景区
            $detail['getApispotListInfo'] = $spobj->getApispotListInfo(
                ['a.status' => ['eq', 1]], ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id', 'a.uuid', 'a.spot_title']
            );

            // 分销商
            $conditionSL['a.id']        = ['in', (new ApiSupplierModel())->getSupplierIdArr()];
            $conditionSL['a.status']    = ['eq', 1];
            $detail['getApiSupplierListInfo'] = $suobj->getApiSupplierListInfo(
                $conditionSL, ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id','a.uuid', 'a.supplier_title']
            );

            $detail['idTypeArr'] = self::$idTypeArr;
            $detail['resumeSexArr'] = self::$resumeSexArr;

            $this->assign('detail', $detail);

            return $this->fetch();
        }

        /**
         * Func 批量添加
         */
        public function adds()
        {
            $spobj = new ApiSpotModel();
            $suobj = new ApiSupplierModel();
            $probj = new ApiProjectModel();
            $srobj = new ApiResumeModel();
            $vailDate = new CommonValidate();

            if ($this->request->isPost())
            {
                $param = $this->request->param();

                $result = $this->validate($param, 'ApiResume.adds');
                if (!is_bool($result))
                {
                    $this->error($result);
                }
                if(!isset($param['visitors']) || count($param['visitors']) <=0)
                {
                    $this->error('请添加游客信息');
                }

                // 手机号过滤验证
                if(isset($param['resume_mobile']) && $param['resume_mobile'])
                {
                    $getIdNumberArr = (new ApiWordModel())->getIdNumberArr(['a.id_type' => 3, 'a.status' => 1]);

                    if($getIdNumberArr)
                    {
                        $getMobileQuery = $this->getMobileQuery(['mobile'=>$param['resume_mobile']]);
                        if (!$getMobileQuery['status'])
                        {
                            $this->error($getMobileQuery['message']);
                        }
                        if(in_array($getMobileQuery['data']['city'],$getIdNumberArr))
                        {
                            $this->error('手机号不能录入，请联系平台运营');
                        }
                    }
                }

                // 验证证件号重复
                $resumeNunberArr = array_column($param['visitors'],'resume_number');
                $unique = array_unique($resumeNunberArr);
                $duplicates = array_diff_assoc($resumeNunberArr, $unique);
                foreach ($duplicates as $val) {
                    $this->error('证件号【'.$val.'】重复');
                }

                // 景区
                $conditionTO['a.uuid']     = ['eq', $param['spot_uuid']];
                $conditionTO['a.status'] = ['eq', 1];
                $getApispotOneInfo = $spobj->getApispotOneInfo(
                    $conditionTO, ['a.id' => 'desc'], ['a.id', 'a.spot_title']
                );
                if (!$getApispotOneInfo)
                {
                    $this->error('景区不存在或已关闭');
                }

                // 项目
                $getApiProjectOneInfo = $probj->getApiProjectOneInfo(
                    ['uuid' => $param['project_uuid']], ['a.id' => 'desc'], ['a.id', 'a.project_total_num', 'a.project_join_num']
                );
                if(!$getApiProjectOneInfo)
                {
                    $this->error('该日期未发布出团');
                }
                $availableCount = (int)($getApiProjectOneInfo['project_total_num'] - $getApiProjectOneInfo['project_join_num']);
                if($availableCount < count($param['visitors']))
                {
                    $this->error('该团队剩余'.$availableCount.'人');
                }

                // 分销商
                $conditionSO['a.uuid']     = ['eq', $param['supplier_uuid']];
                $conditionSO['a.status'] = ['eq', 1];
                $getApiSupplierOneInfo = $suobj->getApiSupplierOneInfo(
                    $conditionSO, ['a.id' => 'desc'], ['a.id','a.user_id', 'a.supplier_title']
                );
                if (!$getApiSupplierOneInfo)
                {
                    $this->error('分销商不存在或已关闭');
                }

                // 验证证件
                foreach ($param['visitors'] as $item)
                {

                    // 年龄限制
                    if($item['resume_age'] < 1 || $item['resume_age'] >100){
                        $this->error($item['resume_name'].'年龄区间在1-100');
                    }

                    // 证件
                    if (!in_array($item['id_type'], array_keys(self::$idTypeArr)))
                    {
                        $this->error($item['resume_name'].'证件类型值错误');
                    }

                    // 身份证格式验证
                    if($item['id_type'] == 1 && !$vailDate->checkIdCard($item['resume_number']))
                    {
                        $this->error($item['resume_name'].'身份证格式错误');
                    }
                    // 身份证格式验证
                    if($item['id_type'] == 2 && !$vailDate->checkPassport($item['resume_number']))
                    {
                        $this->error($item['resume_name'].'护照格式错误');
                    }

                    // 过滤证件号码
                    $conditionId['a.id_type'] = ['eq', $item['id_type']];
                    $conditionId['a.status'] = ['eq', 1];
                    $getIdNumberArr = (new ApiWordModel())->getIdNumberArr($conditionId);
                    if($getIdNumberArr)
                    {
                        $useIdNumberArr = [];
                        for ($i = 1; $i <= strlen($item['resume_number']); $i++)
                        {
                            $useIdNumberArr[] = substr($item['resume_number'], 0, $i);
                        }
                        $intersection = array_intersect($getIdNumberArr,$useIdNumberArr);
                        if($intersection)
                        {
                            $this->error($item['resume_name'].'该证件号不能录入，请联系平台运营');
                        }
                    }

                    // 数据是否录入
                    $conditionRC['a.spot_id']   = array('eq', $getApispotOneInfo['id']);
                    $conditionRC['a.resume_date']   = array('eq', trim($param['resume_date']));
                    $conditionRC['a.resume_number'] = array('eq', trim($item['resume_number']));
                    $getApiResumeOneInfo1 = $srobj->getApiResumeOneInfo($conditionRC,['a.id'=>'desc'],['id']);
                    if ($getApiResumeOneInfo1)
                    {
                        $this->error($item['resume_name'].'证件号已录入');
                    }
                }

                // 批量添加
                $addData = [];
                foreach ($param['visitors'] as $val)
                {
                    $addData[] = [
                        'uuid'          => uuid(),
                        'resume_name'   => $val['resume_name'],
                        'resume_sex'    => $val['resume_sex'],
                        'resume_age'    => $val['resume_age'],
                        'id_type'       => $val['id_type'],
                        'resume_number' => $val['resume_number'],
                        'resume_mobile' => $param['resume_mobile'],
                        'resume_date'   => $param['resume_date'],
                        'resume_desc'   => '',
                        'user_id'       => $getApiSupplierOneInfo['user_id'],
                        'spot_id'       => $getApispotOneInfo['id'],
                        'supplier_id'   => $getApiSupplierOneInfo['id'],
                        'project_id'    => $getApiProjectOneInfo['id'],
                        'handle_time'   => time(),
                        'create_time'   => time(),
                        'update_time'   => time(),
                        'status'        => 1,
                    ];
                }
                $result = Db::name('api_resume')->insertAll($addData);
                if ($result)
                {
                    $probj->triggerProjectUpdateData($getApiProjectOneInfo['id']);
                    $this->success('添加成功!', url('index', []));
                } else {
                    $this->error('操作失败，请稍后重试');
                }
            }

            // 景区
            $detail['getApispotListInfo'] = $spobj->getApispotListInfo(
                ['a.status' => ['eq', 1]], ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id', 'a.uuid','a.spot_title']
            );

            // 分销商
            $conditionSL['a.id']        = ['in', (new ApiSupplierModel())->getSupplierIdArr()];
            $conditionSL['a.status']    = ['eq', 1];
            $detail['getApiSupplierListInfo'] = $suobj->getApiSupplierListInfo(
                $conditionSL, ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id', 'a.uuid','a.supplier_title']
            );

            $detail['idTypeArr'] = self::$idTypeArr;
            $detail['resumeSexArr'] = self::$resumeSexArr;

            $this->assign('detail', $detail);

            return $this->fetch();
        }

        /**
         * Func 修改
         */
        public function edit()
        {
            $param = $this->request->param();

            $spobj = new ApispotModel();
            $reobj = new ApiResumeModel();
            $suobj = new ApiSupplierModel();

            $detail = $reobj->getApiResumeOneInfo(['a.uuid' => $param['uuid']], ['a.id' => 'desc']);
            if (!$detail) $this->error('信息不存在');

            if ($this->request->isPost())
            {
                $result = $this->validate($param, 'ApiResume.edit');
                if (!is_bool($result))
                {
                    $this->error($result);
                }

                if (!in_array($param['id_type'], array_keys(self::$idTypeArr)))
                {
                    $this->error('证件类型值错误');
                }

                $vailDate = new CommonValidate();

                // 身份证格式验证
                if($param['id_type'] == 1 && !$vailDate->checkIdCard($param['resume_number']))
                {
                    $this->error('身份证格式错误');
                }
                // 身份证格式验证
                if($param['id_type'] == 2 && !$vailDate->checkPassport($param['resume_number']))
                {
                    $this->error('护照格式错误');
                }

                // 过滤证件号码
                $conditionId['a.id_type'] = ['eq', $param['id_type']];
                $conditionId['a.status'] = ['eq', 1];
                $getIdNumberArr = (new ApiWordModel())->getIdNumberArr($conditionId);
                if($getIdNumberArr)
                {
                    $useIdNumberArr = [];
                    for ($i = 1; $i <= strlen($param['resume_number']); $i++)
                    {
                        $useIdNumberArr[] = substr($param['resume_number'], 0, $i);
                    }
                    $intersection = array_intersect($getIdNumberArr,$useIdNumberArr);
                    if($intersection)
                    {
                        $this->error('该证件号不能录入，请联系平台运营');
                    }
                }

                // 景区
                $conditionTO['a.uuid']   = ['eq', $param['spot_uuid']];
                $conditionTO['a.status'] = ['eq', 1];
                $getApispotOneInfo = $spobj->getApispotOneInfo(
                    $conditionTO, ['a.id' => 'desc'], ['a.id', 'a.spot_title']
                );
                if (!$getApispotOneInfo)
                {
                    $this->error('景区不存在或已关闭');
                }

                // 分销商
                $conditionSO['a.uuid']   = ['eq', $param['supplier_uuid']];
                $conditionSO['a.status'] = ['eq', 1];
                $getApiSupplierOneInfo = $suobj->getApiSupplierOneInfo(
                    $conditionSO, ['a.id' => 'desc'], ['a.id','a.user_id', 'a.supplier_title']
                );
                if (!$getApiSupplierOneInfo)
                {
                    $this->error('分销商不存在或已关闭');
                }

                // 查询数据是否存在
                $conditionRC['a.id']   = array('neq', $detail['id']);
                $conditionRC['a.spot_id']   = array('eq', $getApispotOneInfo['id']);
                $conditionRC['a.resume_date']   = array('eq', trim($param['resume_date']));
                $conditionRC['a.resume_number'] = array('eq', trim($param['resume_number']));
                $getApiResumeOneInfo1 = $reobj->getApiResumeOneInfo($conditionRC,['a.id'=>'desc'],['id']);
                if ($getApiResumeOneInfo1 && $getApiResumeOneInfo1['id'] != $getApiSupplierOneInfo['id'])
                {
                    $this->error('【'.$param['resume_date'].'】证件号已存在');
                }

                // 添加和更新
                $data1 = self::getPostParamInfo([
                    'resume_name', 'resume_sex', 'resume_age','id_type', 'resume_number',
                    'resume_mobile', 'resume_date', 'resume_desc'
                ]);

                // 附加参数
                $data2['spot_id']       = $getApispotOneInfo['id'];
                $data2['supplier_id']   = $getApiSupplierOneInfo['id'];
                $data2['user_id']       = $getApiSupplierOneInfo['user_id'];

                // 审核状态
                if(session('admin_is_operate') == 1)
                {
                    if($detail['status'] != $param['status'] && in_array($param['status'],[-1,2,3]))
                    {
                        $data2['project_id'] = 0;
                        $data2['status'] = $param['status'];
                    }
                } else {
                    if($detail['status'] != 3)
                    {
                        $this->error('只有待审核状态才能修改');
                    }
                }

                $data = array_merge((array)$data1, (array)$data2);
                if ($reobj->addOrUpdateApiResumeInfo($data, $detail['id']))
                {
                    if(session('admin_is_operate') == 1)
                    {
                        if($detail['project_id'] > 0)
                        {
                            (new ApiProjectModel())->triggerProjectUpdateData($detail['project_id']);
                        }
                    }
                    $this->success('修改成功!', url('index', []));
                }else {
                    $this->error('操作失败,请稍后重试');
                }
            }

            // 景区
            $detail['getApispotListInfo'] = $spobj->getApispotListInfo(
                ['a.status' => ['eq', 1]], ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id','a.uuid', 'a.spot_title']
            );

            // 分销商
            $conditionSL['a.id']        = ['in', (new ApiSupplierModel())->getSupplierIdArr()];
            $conditionSL['a.status']    = array('eq', 1);
            $detail['getApiSupplierListInfo'] = $suobj->getApiSupplierListInfo(
                $conditionSL, ['a.id' => 'desc'], ['page' => 1, 'limit' => 500],
                ['a.id','a.uuid', 'a.supplier_title']
            );

            $detail['idTypeArr'] = self::$idTypeArr;
            $detail['resumeSexArr']     = self::$resumeSexArr;
            $detail['resumeStatusArr']  = self::$resumeStatusArr;

            $this->assign ( 'detail' , (array)$detail );

            return $this->fetch();
        }

        /**
         * Func 旅行社人员统计
         */
        public function indexl()
        {
            $param = $this->request->param();

            // 时间
            if ($this->start_time && $this->end_time)
            {
                $condition['a.resume_date'] = ['between', [
                    date('Y-m_d',strtotime($this->start_time)), date('Y-m-d',strtotime($this->end_time)),
                ]];
            }

            // 关键词
            if ($this->keyword)
            {
                if ($this->searchType)
                {
                    $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
                } else {
                    $condition['a.resume_name|b.spot_title|c.project_title|d.company_title'] = ['like', '%' . $this->keyword . '%'];
                }
            }

            // 游玩日期
            if ($this->play_date)
            {
                $condition['a.resume_date'] = ['eq', $this->play_date];
            }

            // 可查看的需求id
            $projectIdArr = (new ApiProjectModel())->getProjectIdArr();

            $condition['a.id'] = ['egt', 0];
            $condition['a.project_id'] = ['in', $projectIdArr];
            $condition['a.status'] = ['eq', 1];

            // 连接表
            $joinArr = [
                ['api_spot b', 'a.spot_id = b.id', 'left'],
                ['api_project c', 'a.project_id = c.id', 'left'],
                ['api_company d', 'c.company_id = d.id', 'left'],
            ];

            $reobj = new ApiResumeModel();

            // 导出
            if (isset($param['action']) && $param['action'] == 1)
            {
                // 字段
                $fieldArr = [
                    'b.spot_title','d.company_title','c.project_title','a.resume_date',
                    'a.resume_name','a.resume_mobile','a.resume_sex',
                    'a.resume_age','a.resume_number'
                ];
                $infos = $reobj->getApiResumeListInfo(
                    $condition, ['a.id' => 'desc'], ['page' => 1, 'limit' =>1000000],
                    $fieldArr, $joinArr
                );
                $header = [
                    '景区', '旅行社', '团队名称', '游玩日期', '姓名','联系电话','性别', '年龄', '证件号'
                ];

                $this->exportCsv('旅行社游客_' . date('YmdHis'), $header, $infos);
            }else{

                $fieldArr =  ['a.*','b.spot_title','c.project_title','d.company_title'];
                $infos['list'] = $reobj->getApiResumeListInfo(
                    $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
                    $fieldArr, $joinArr
                );
                $infos['passCount'] = $reobj->getApiResumeCount($condition,$joinArr); // 已通过

                $infos['idTypeArr'] = self::$idTypeArr;
                $infos['resumStatusArr'] = self::$resumeStatusArr;

                $this->assign('infos', $infos);

                return $this->fetch();
            }
        }


        /**
         * Func 分销商人员统计
         */
        public function indexs()
        {
            $param = $this->request->param();

            // 时间
            if ($this->start_time && $this->end_time)
            {
                $condition['a.resume_date'] = ['between', [
                    date('Y-m_d',strtotime($this->start_time)), date('Y-m-d',strtotime($this->end_time)),
                ]];
            }

            // 关键词
            if ($this->keyword)
            {
                if ($this->searchType)
                {
                    $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
                } else {
                    $condition['a.resume_name'] = ['like', '%' . $this->keyword . '%'];
                }
            }

            // 游玩日期
            if ($this->play_date)
            {
                $condition['a.resume_date'] = ['eq', $this->play_date];
            }

            // 可查看的分销商id
            $supplierIdArr = (new ApiSupplierModel())->getSupplierIdArr();

            $condition['a.id'] = ['egt', 0];
            $condition['a.supplier_id'] = ['in', $supplierIdArr];
            $condition['a.status'] = ['eq', 1];



            // 连接表
            $joinArr = [
                ['api_spot b', 'a.spot_id = b.id', 'left'],
                ['api_supplier c', 'a.supplier_id = c.id', 'left'],
            ];

            $reobj = new ApiResumeModel();

            // 导出
            if (isset($param['action']) && $param['action'] == 1)
            {
                // 字段
                $fieldArr = [
                    'b.spot_title','c.supplier_title','a.resume_date','a.resume_name',
                    'a.resume_mobile','a.resume_sex','a.resume_age','a.resume_number'
                ];
                $infos = $reobj->getApiResumeListInfo(
                    $condition, ['a.id' => 'desc'], ['page' => 1, 'limit' => 1000000],
                    $fieldArr, $joinArr
                );
                $header = [
                    '景区', '分销商', '游玩日期', '姓名', '联系电话','性别', '年龄', '证件号'
                ];
                $this->exportCsv('分销商游客_' . date('YmdHis'), $header, $infos);
            }else{
                // 字段
                $fieldArr = ['a.*','b.spot_title','c.supplier_title'];
                $infos['list'] = $reobj->getApiResumeListInfo(
                    $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
                    $fieldArr, $joinArr
                );

                $infos['passCount'] = $reobj->getApiResumeCount($condition,$joinArr); // 已通过

                $infos['idTypeArr'] = self::$idTypeArr;
                $infos['resumStatusArr'] = self::$resumeStatusArr;

                $this->assign('infos', $infos);

                return $this->fetch();
            }
        }


        /**
         * Func 旅行社游客列表
         */
        public function indexla()
        {
            $param = $this->request->param();

            // 时间
            if ($this->start_time && $this->end_time)
            {
                $condition['a.create_time'] = ['between', [
                    strtotime($this->start_time), strtotime($this->end_time),
                ]];
            }

            // 关键词
            if ($this->keyword)
            {
                if ($this->searchType)
                {
                    $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
                } else {
                    $condition['a.resume_name'] = ['like', '%' . $this->keyword . '%'];
                }
            }

            // 游玩日期
            if ($this->play_date)
            {
                $condition['a.resume_date'] = ['eq', $this->play_date];
            }

            // 状态
            if ($this->status)
            {
                $condition['a.status'] = ['eq', $this->status];
            }

            $condition['a.id'] = ['egt', 0];

            // 字段
            $fieldArr = ['a.*', 'b.user_login','c.supplier_title','d.spot_title',];

            // 连接表
            $joinArr = [
                ['user b', 'a.user_id = b.id', 'left'],
                ['api_supplier c', 'a.supplier_id = c.id', 'left'],
                ['api_spot d', 'a.spot_id = d.id', 'left'],
            ];

            $reobj = new ApiResumeModel();

            $infos['list'] = $reobj->getApiResumeListInfo(
                $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
                $fieldArr, $joinArr
            );

            $infos['idTypeArr'] = self::$idTypeArr;
            $infos['resumStatusArr'] = self::$resumeStatusArr;

            $this->assign('infos', $infos);

            return $this->fetch();
        }

        /**
         * 通用CSV导出方法
         * @param string $fileName 文件名（不含后缀）
         * @param array $header 表头
         * @param array $data 数据
         */
        private function exportCsv($fileName = "", $header = [], $data = [])
        {
            header('Content-Type: application/csv');
            header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
            header('Cache-Control: max-age=0');
            header('Expires: 0');
            header('Pragma: public');
            $output = fopen('php://output', 'w');
            fwrite($output, chr(0xEF).chr(0xBB).chr(0xBF));
            fputcsv($output, $header);
            foreach ($data as $row) {
                fputcsv($output, $row);
            }
            fclose($output);
            exit;
        }

    }